package com.genius.reservation.common.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

//@Configuration

public class data2 {


    @Bean(name = "db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.d2")
    @Qualifier("db2DataSource")
    public DataSource db2DataSource() {
        return DruidDataSourceBuilder.create().build();
    }
        @Bean(name = "db2SqlSessionFactory")
        public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource datasource)
                throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(datasource);
            //TODO:配置两个数据库的问题
            bean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("classpath*:genius/reservation/mapper/*"));
            return bean.getObject();
        }



//
//        @Bean(name="db2transactionManager")
//        public DataSourceTransactionManager transactionManagerOne(){
//            return  new DataSourceTransactionManager(db3DataSource());
//        }
//
//        @Bean(name = "db2JdbcTemplate")
//        public JdbcTemplate jdbcTemplate(
//                @Qualifier("db2DataSource") DataSource dataSource) {
//            return new JdbcTemplate(dataSource);
//        }


}
